// action ->  mutation -> state
import * as types from '../types'
import store from 'store'
import { LoginByUserName, getInfo, logout } from '@/api/index'
import { storeExpire } from '@/config/index'

const state = {
  user: '',
  status: '',
  email: '',
  code: '',
  uid: undefined,
  auth_type: '',
  token: '',
  name: '',
  avatar: '',
  introduction: '',
  roles: [],
  setting: {

  }
}

const mutations = {
  [types.SET_USER_AUTH_TYPE](state, type){
    state.auth_type = type;
  },
  [types.SET_USER_CODE](state, code){
    state.code = code;
  },
  [types.SET_USER_TOKEN](state, { token }){
    state.token = token;
  },
  [types.SET_USER_UID](state, uid){
    state.uid = uid;
  },
  [types.SET_USER_EMAIL](state, email){
    state.email = email;
  },
  [types.SET_USER_INTRODUCTION](state, introduction){
    state.introduction = introduction;
  },
  [types.SET_USER_SETTING](state, setting){
    state.setting = setting;
  },
  [types.SET_USER_STATUS](state, status){
    state.status = status;
  },
  [types.SET_USER_NAME](state, name){
    state.name = name;
  },
  [types.SET_USER_AVATAR](state, avatar){
    state.avatar = avatar;
  },
  [types.SET_USER_ROLES](state, roles){
    state.roles = roles;
  },
  [types.LOGIN_SUCCESS](){
    console.log('login success')
  },
  [types.LOGOUT_USER](state){
    state.user = '';
  }
}

const actions = {

  // 登录
  LoginByUserName({ commit }, userInfo) {
    const username = userInfo.username.trim();
    return new Promise((resolve, reject) => {
      LoginByUserName(username, userInfo.password).then(response => {
        const data = response.data;

        commit( types.SET_USER_TOKEN, { __store: { expire: storeExpire }, token: data.token });
        commit( types.SET_USER_NAME, username);
        resolve();
      }).catch(error => {
        reject(error);
      });
    });
  },

  // 获取用户信息
  GetInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const data = response.data;
        commit(types.SET_USER_ROLES, data.role);
        commit(types.SET_USER_NAME, data.name);
        commit(types.SET_USER_UID, data.uid);
        commit(types.SET_USER_INTRODUCTION, data.introduction);
        resolve(response);
      }).catch(error => {
        reject(error);
      });
    });
  },

  // 第三方验证登录
  LoginByThirdparty({ commit, state }, code) {
    return new Promise((resolve, reject) => {
      commit(types.SET_USER_CODE, code);
      loginByThirdparty(state.status, state.email, state.code, state.auth_type).then(response => {
        commit( types.SET_USER_TOKEN, { __store: { expire: storeExpire }, token: response.data.token });
        resolve();
      }).catch(error => {
        reject(error);
      });
    });
  },


  // 登出
  LogOut({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit(types.SET_USER_TOKEN, { token: ''});
        commit(types.SET_USER_ROLES, []);
        store.clearAll()
        resolve();
      }).catch(error => {
        reject(error);
      });
    });
  },

  // 前端 登出
  FedLogOut({ commit }) {
    return new Promise(resolve => {
      commit(types.SET_USER_TOKEN, { token: ''});
      store.clearAll()
      resolve();
    });
  }
}

export default {
    state,
    mutations,
    actions
}
